package edu.yctc.weblog.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.yctc.weblog.constant.ConstantHolder;
import edu.yctc.weblog.constant.ResultCode;
import edu.yctc.weblog.dao.ConfigDAO;
import edu.yctc.weblog.entity.ConfigDO;
import edu.yctc.weblog.entity.ResultDO;
import edu.yctc.weblog.service.ConfigService;

@Service("configService")
public class ConfigServiceImpl implements ConfigService {

    public static final String WEBSITE_NAME = "websiteName";
    public static final String WEBSITE_DESCRIPTION = "websiteDescription";
    public static final String WEBSITE_LOGO = "websiteLogo";
    public static final String WEBSITE_ICON = "websiteIcon";
    public static final String AVATAR = "yourAvatar";
    public static final String EMAIL = "yourEmail";
    public static final String NAME = "yourName";
    public static final String FOOTER_ABOUT = "footerAbout";
    public static final String FOOTER_ICP = "footerICP";
    public static final String FOOTER_COPY_RIGHT = "footerCopyRight";
    public static final String FOOTER_POWERED_BY = "footerPoweredBy";
    public static final String FOOTER_POWERED_BY_URL = "footerPoweredByURL";

    private final static Logger LOG = LoggerFactory.getLogger("serviceLogger");

    @Resource
    private ConfigDAO configDAO;

    @Override
    @Transactional
    public ResultDO<Integer> updateConfig(String configName, String configValue) {
        if (StringUtils.isBlank(configName) || StringUtils.isBlank(configValue)) {
            LOG.error("update config fail, parameter invlaid, configName={}, configValue={}", configName, configValue);
            return new ResultDO<Integer>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID,
                -ResultCode.PARAMETER_INVALID);
        }
        ConfigDO configDO = configDAO.getConfigDOByName(configName);
        if (configDO == null) {
            LOG.error("get configDO by name fail, no such config, name={}", configName);
            return new ResultDO<Integer>(false, ResultCode.DB_ERROR, ResultCode.MSG_DB_ERROR, -ResultCode.DB_ERROR);
        }
        configDO.setValue(configValue);
        configDAO.update(configDO);
        LOG.info("update config success, configName={}, configValue", configName, configValue);
        return new ResultDO<Integer>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, ResultCode.SUCCESS);
    }

    @Override
    public ResultDO<Map<String, String>> getAllConfigs() {
        List<ConfigDO> configDOs = configDAO.getAllConfigDOs();
        Map<String, String> configMap =
            configDOs.stream().collect(Collectors.toMap(ConfigDO::getName, ConfigDO::getValue));
        for (Map.Entry<String, String> config : configMap.entrySet()) {
            if (WEBSITE_NAME.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.WEBSITE_NAME);
            }
            if (WEBSITE_DESCRIPTION.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.WEBSITE_DESCRIPTION);
            }
            if (WEBSITE_LOGO.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.WEBSITE_LOGO);
            }
            if (WEBSITE_ICON.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.WEBSITE_ICON);
            }
            if (AVATAR.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.AVATAR);
            }
            if (EMAIL.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.EMAIL);
            }
            if (NAME.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.NAME);
            }
            if (FOOTER_ABOUT.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.FOOTER_ABOUT);
            }
            if (FOOTER_ICP.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.FOOTER_ICP);
            }
            if (FOOTER_COPY_RIGHT.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.FOOTER_COPY_RIGHT);
            }
            if (FOOTER_POWERED_BY.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.FOOTER_POWERED_BY);
            }
            if (FOOTER_POWERED_BY_URL.equals(config.getKey()) && StringUtils.isBlank(config.getValue())) {
                config.setValue(ConstantHolder.FOOTER_POWERED_BY_URL);
            }
        }
        return new ResultDO<Map<String, String>>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, configMap);
    }

}
